<template>
  <div class="goods-list-container">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input @clear="getGoodsList" clearable placeholder="请输入内容" v-model="paramsInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <el-table
        border
        ref="singleTable"
        :data="goodsList"
        style="width: 100%">
        <el-table-column
          type="index"
          width="50">
        </el-table-column>
        <el-table-column
          property="goods_name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          width="120px"
          property="goods_price"
          label="商品价格(元)">
        </el-table-column>
        <el-table-column
          width="100px"
          property="goods_weight"
          label="商品重量">
        </el-table-column>
        <el-table-column
          width="180px"
          label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作"  width="130px">
          <template slot-scope="scope">
            <el-button size="mini" icon="el-icon-edit" type="primary"></el-button>
            <el-button size="mini" icon="el-icon-delete" type="danger" @click="delGood(scope.row.goods_id)"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paramsInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="paramsInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [],
      paramsInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.paramsInfo
      })

      if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    handleSizeChange(val) {
      this.paramsInfo.pagesize = val
      this.getGoodsList()
    },
    handleCurrentChange(val) {
        this.paramsInfo.pagenum = val
      this.getGoodsList()
    },
    delGood(id) {
      this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const { data: res } = await this.$http.delete(`goods/${id}`)

        if(res.meta.status !== 200) return this.$message.error(res.meta.msg)

        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getGoodsList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })         
      })
    },
    goAddPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less">
  .goods-list-container{
    .box-card{
      margin-top: 20px;
    }
    .el-table{
      margin: 20px 0;
    }
  }
</style>